package com.baseDemo.web.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;


/**
 * 导航菜单数据库持久类
 * @author xianwei
 *
 */
@Entity
@Table(name = "t_NavigationEntity")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE,region="NavigationEhcache")
//Json 转换时过滤掉的字段   序列化json字符串为java对象时  不转换没有的字段
@JsonIgnoreProperties(ignoreUnknown=true)
public class NavigationEntity implements Serializable{

	private static final long serialVersionUID = -1415643385754618567L;
	
	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
	@Column(length = 50)
	private String id;
	@Column(length = 50)
	private String appName;
	@Column(length = 50)
	private String text;
	@Column(length = 50)
	private String icon;
	@Column(length = 50)
	private String controller;
	@Column(length = 50)
	private String clazz;
	@Column(length = 50)
	private String parentId;
	/**
	 * 数据库中hibernate配置Boolean
	 * 如果你的class中用的boolean，数据库中用varchar，把映射文件中property的type写成yes_no，数据库保存的会是Y和N，执行hql时，hibernate会把Y和true，N和false相互转换,<property name="visible" type="yes_no" />。
	 * 如果你的class中用的boolean，数据库中用的number，把映射文件中property的type写成byte，数据库保存的会是1和0，执行hql时，hibernate会把1和true，0和false互相转换，<property name="visible" type="byte" />。
	 */
	@Column(name="leaf")
	@Type(type="yes_no")
	private boolean leaf;
	//单项一对多
	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=ToolbarMenuEntity.class ,fetch=FetchType.LAZY)
	@OrderBy("seq")
	@JoinColumn(name="navigationid") 
	//@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
	private Set<ToolbarMenuEntity> toolbarMenuEntities = new HashSet<ToolbarMenuEntity>();
	
	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=AuthorityEntity.class )
	@JoinColumn(name="navigationid") 
	@JsonIgnore
	private Set<AuthorityEntity> authorities = new HashSet<AuthorityEntity>();
	 /**
	  * @ManyToOne：多对一,cascade：级联,请参考上一篇
      * fetch = FetchType.LAZY,延迟加载策略,如果不想延迟加载可以用FetchType.EAGER
      */
 /*   @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch = FetchType.EAGER)
    @JoinColumn(name = "parent")
    //转化为json字符串时  过滤此字段
    @JsonIgnore
	private NavigationEntity navigationEntity;*/
    
    /**
     * @OneToMany 与 OneToOne相似的也用mappedBy,
     * 可以参考上一篇
     */
   // @OneToMany(mappedBy = "navigationEntity")
	@Transient
	private List<NavigationEntity> children = new ArrayList<NavigationEntity>();
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * @return the appName
	 */
	public String getAppName() {
		return appName;
	}
	/**
	 * @param appName the appName to set
	 */
	public void setAppName(String appName) {
		this.appName = appName;
	}
	/**
	 * @return the text
	 */
	public String getText() {
		return text;
	}
	/**
	 * @param text the text to set
	 */
	public void setText(String text) {
		this.text = text;
	}
	/**
	 * @return the icon
	 */
	public String getIcon() {
		return icon;
	}
	/**
	 * @param icon the icon to set
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}
	/**
	 * @return the controller
	 */
	public String getController() {
		return controller;
	}
	/**
	 * @param controller the controller to set
	 */
	public void setController(String controller) {
		this.controller = controller;
	}
	/**
	 * @return the clazz
	 */
	public String getClazz() {
		return clazz;
	}
	/**
	 * @param clazz the clazz to set
	 */
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}
	/**
	 * @return the leaf
	 */
	public boolean isLeaf() {
		return leaf;
	}
	/**
	 * @param leaf the leaf to set
	 */
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
	
	/**
	 * @return the parent
	 */
	public String getParentId() {
		return parentId;
	}
	/**
	 * @param parent the parent to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
/*	*//**
	 * @return the parentNode
	 *//*
	public NavigationEntity getParentNode() {
		return parentNode;
	}
	*//**
	 * @param parentNode the parentNode to set
	 *//*
	public void setParentNode(NavigationEntity parentNode) {
		this.parentNode = parentNode;
	}*/
	/**
	 * @return the children
	 */
	public List<NavigationEntity> getChildren() {
		return children;
	}
	/**
	 * @param children the children to set
	 */
	public void setChildren(List<NavigationEntity> children) {
		this.children = children;
	}
	/**
	 * @return the toolbarMenuEntities
	 */
	public Set<ToolbarMenuEntity> getToolbarMenuEntities() {
		return toolbarMenuEntities;
	}
	/**
	 * @param toolbarMenuEntities the toolbarMenuEntities to set
	 */
	public void setToolbarMenuEntities(Set<ToolbarMenuEntity> toolbarMenuEntities) {
		this.toolbarMenuEntities = toolbarMenuEntities;
	}
	public Set<AuthorityEntity> getAuthorities() {
		return authorities;
	}
	public void setAuthorities(Set<AuthorityEntity> authorities) {
		this.authorities = authorities;
	}
	
	

}
